﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using WPFExcelReport.Helpers;
using System.ComponentModel;
namespace WPFExcelReport
{
    public partial class TGGS08
    {

        public TGGS08()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VGASTGA();
            DataContext = MV;
            InitializeComponent();
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster == null)
            {

            }
            else
            {

            }
        }
        string GART = "";
        public override void OnEXAUFChg(string vlaue)
        {
            TBMain.Clear();
            if (EXAUF.Length < 33)
                return;
            dwData = TBMain.NewRow();
            dwData["GAGMANDANT"] = app.Mandant;
            dwData["GAGART"] = EXAUF.Substring(1, 20);
            dwData["GAGVAR"] = EXAUF.Substring(21, 3);
            dwData["GAGFARBE"] = EXAUF.Substring(24, 10);
            GART = EXAUF;
            SResult = TMV.QGG10(dwData);
            MergeData(SResult, true);
            base.OnEXAUFChg(vlaue);
            if (!IsAuto)
                return;
            if (Counts > 0)
                return;
            dwData = TBMain.NewRow();
            TMV.SetUpNewRow(ref dwData);
            dwData["GAGMANDANT"] = app.Mandant;
            dwData["GAGART"] = EXAUF.Substring(1, 20);
            dwData["GAGVAR"] = EXAUF.Substring(21, 3);
            dwData["GAGFARBE"] = EXAUF.Substring(24, 10);
            dwData["GAGART"] = EXAUF.Substring(1, 20);
            dwData["GAGGVAR"] = EXAUF.Substring(21, 3);
            dwData["GAGGFARBE"] = "".PadRight(10);
            dwData["GAGKGNET"] = "0001000000";
            dwData["GAGKGBRUT"] = dwData["GAGKGNET"];
            dwData["GAGANTEIL"] = 100 * 1000;
            TBMain.Rows.InsertAt(dwData, 0);
            TMV.InsertGASTGA(dwData, app.company);
        }
        public override void LOADBYAUF()
        {
            /*
            if (!IsSaved)
                return;
            if (ORDERNUM.Length < 35)
                return;
            if (ORDERNUM != KEY)
                return;
            dwData = TBMain.NewRow();
            dwData["GAGCMANDANT"] = app.Mandant;
            dwData["GAGART"] = ORDERNUM.Substring(0, 20);
            dwData["GAGVAR"] = ORDERNUM.Substring(20, 3);
            dwData["GAGFARBE"] = ORDERNUM.Substring(23, 10);
            dwData["GAGPOS"] = " AND GAGPOS='" + ORDERNUM.Substring(33, 2) + "'";
            SResult = TMV.QGG10(dwData);
            TBTemp = TBMain.Clone();
            TBTemp.Merge(SResult.Tables[0]);
            foreach (DataRow row in TBTemp.Rows)
            {
                dwMaster.ItemArray = row.ItemArray;
            }
            IsSaved = false;*/
        }
        public override void OnYARNChg()
        {
            if (YARN.Length < 33)
                return;
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            TMV.GAGGART = YARN.Substring(0, 20);
            TMV.GAGGVAR = YARN.Substring(20, 3);
            TMV.GAGGFARBE = YARN.Substring(23, 10);
            base.OnYARNChg();
        }
        public override void EditCommand(object sender, RoutedEventArgs e)
        {
            IsSaved = false;
            base.EditCommand(sender, e);
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            if (!Validate())
                return;
            foreach(DataRow row in TBMain.Rows)
            {
                TMV.UpdateGASTGA(row, app.company, 0);
            }
            base.SaveCommand(sender, e);
        }
        public bool Validate()
        {
            bool IsValidate = true;
            double ratio = 0;
            double Tratio = 0;
            double wastage = 0;
            foreach (DataRow row in TBMain.Rows)
            {
                ratio = Convert.ToDouble(row["GAGANTEIL"].ToString()) * 0.001;
                wastage = Convert.ToDouble(row["GAGABF"].ToString()) * 0.01*0.01;
                Tratio += ratio;
                row["GAGKGNET"] = Helper.Right("0000000000" + (ratio * 10000).ToString(), 10);
                row["GAGKGBRUT"] = Helper.Right("0000000000" + (ratio * (1 + wastage) * 10000).ToString(), 10);
            }
            if (Tratio != 100)
            {
                IsValidate = false;
                MessageBox.Show("比例不等于100%");
            }                
            return IsValidate;
        }
        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
            {
                return;
            }
            filter = "GAGART='" + dwMaster["GAGART"].ToString() + "' AND GAGVAR='" + dwMaster["GAGVAR"].ToString() + "' AND GAGFARBE='" + dwMaster["GAGFARBE"].ToString() + "' AND GAGPOS='" + dwMaster["GAGPOS"].ToString() + "'";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                TMV.DGG01(row);
                row.Delete();
            }
            vwMaster.View.MoveCurrentToFirst();
            base.DeleteCommand(sender, e);
        }
        public override void CopyCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
            {
                MessageBox.Show("清先选择一个记录");
                return;
            } 
             
            dwData = TBMain.NewRow();
            dwData.ItemArray = dwMaster.ItemArray;
            dwData["GAGPOS"] = TMV.GETPOS(dwMaster);
            dwData["GAGGART"] = ART.Trim().PadRight(20);
            dwData["GAGGVAR"] = VAR.Trim().PadRight(3);
            dwData["GAGGFARBE"] = FARBE.Trim().PadRight(10);
            TBMain.Rows.InsertAt(dwData, 0);
            TMV.InsertGASTGA(dwData, app.company); 
            vwMaster.View.MoveCurrentToFirst();
            base.CopyCommand(sender, e);
        }

        public override void NewCommand(object sender, RoutedEventArgs e)
        {
           
            dwData = TBMain.NewRow();
            TMV.SetUpNewRow(ref dwData);
            if(dwMaster!=null)
            {
                dwData["GAGPOS"] = TMV.GETPOS(dwMaster);
            }                
            dwData["GAGMANDANT"] = app.Mandant;
            dwData["GAGART"] = GART.Substring(0, 20);
            dwData["GAGVAR"] = GART.Substring(20, 3);
            dwData["GAGFARBE"] = GART.Substring(23, 10); 
            TBMain.Rows.InsertAt(dwData, 0);
            TMV.InsertGASTGA(dwData, app.company); 
            vwMaster.View.MoveCurrentToFirst();
            base.NewCommand(sender, e);
        }

       
    }
}
